window.addEventListener("load", onload);
function onload(){
	//addButton();
	//add event listeners to existing buttons
	var btnAdd=document.querySelector("#btnAdd");
	var btnRemove=document.querySelector("#btnRemove");
	btnAdd.addEventListener("click", addNewButton, false);
	btnRemove.addEventListener("click", removeNewButton, false);

}
//add button handler -creates,initialise, adds new button, and saves reference to it for further use

var newButtons=[];
function addNewButton(){
	var newButton=document.createElement("button");
	newButton.setAttribute("type", "button");
	newButton.innerHTML="new";
	var divHtml=document.querySelector("div");
	divHtml.appendChild(newButton);
	newButtons.push(newButton);
	
}
//remove button handler- checks if button already added(checks reference), if added removes button
function removeNewButton(){
	if(newButtons.length==0)return;
	var divHtml=document.querySelector("div");
	for(var i=0; i<newButtons.length; i++){
		divHtml.removeChild(newButtons[i]);
	}
	newButtons=[];
	
}
var addedButton, divHtml, anotherButton, buttonAdd;

function addButton(){
	addedButton=document.createElement("button");
	addedButton.setAttribute("type", "button");
	addedButton.innerHTML="hello";
	addedButton.addEventListener("click", hideButton, false);
	divHtml=document.querySelector("div");
	divHtml.appendChild(addedButton);
}
function hideButton(){
	divHtml.removeChild(addedButton);
}
